package com.aistar.service.reply.impl;

import com.aistar.mapper.*;
import com.aistar.pojo.*;
import com.aistar.service.reply.ReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.ognl.InappropriateExpressionException;
import org.aspectj.weaver.patterns.TypePatternQuestions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author shkstart
 * @date 2019/10/6 - 0:01
 */
@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private TravelnoteMapper travelnoteMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private TracommMapper tracommMapper;
    @Autowired
    private AncommMapper ancommMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public List<Map> reply(int id) {
//        所有返回数据全部放入list集合
        List list = new ArrayList();
//        根据登陆用户id查询其所写游记
        TravelnoteExample travelnoteExample = new TravelnoteExample();
        TravelnoteExample.Criteria criteria11 = travelnoteExample.createCriteria();
        criteria11.andTravWidEqualTo(id);
//        再根据游记id查询相应的游记评论
        TracommExample tracommExample = new TracommExample();
        TracommExample.Criteria criteria1 = tracommExample.createCriteria();
        List<Travelnote> list11 = travelnoteMapper.selectByExampleWithBLOBs(travelnoteExample);
        Iterator<Travelnote> iterator11 = list11.iterator();
        while (iterator11.hasNext()){
            Travelnote note = iterator11.next();
            criteria1.andTracTableIdEqualTo(note.getTravId());
//            List<Tracomm> list1 = tracommMapper.selectByExample(tracommExample);
            List<Tracomm> list1 = tracommMapper.selectByExampleWithBLOBs(tracommExample);
            Iterator<Tracomm> iterator1 = list1.iterator();
            while (iterator1.hasNext()){
                Map<Travelnote,Member> map1 = new HashMap<Travelnote,Member>();
                Map<Tracomm,Map<Travelnote,Member>> map = new HashMap<Tracomm,Map<Travelnote,Member>>();
                Tracomm tracomm = iterator1.next();
                Member member = memberMapper.selectByPrimaryKey(tracomm.getTracUserId());
                map1.put(note,member);
                map.put(tracomm,map1);
                list.add(map);
                System.out.println("TravelNote");
            }
        }
        QuestionExample questionExample = new QuestionExample();
        QuestionExample.Criteria criteria22 = questionExample.createCriteria();
        criteria22.andQueUserIdEqualTo(id);
        AnswerExample answerExample = new AnswerExample();
        AnswerExample.Criteria criteria2 = answerExample.createCriteria();
        List<Question> list22 = questionMapper.selectByExampleWithBLOBs(questionExample);
        Iterator<Question> iterator22 = list22.iterator();
        while (iterator22.hasNext()){
            Question question = iterator22.next();
            criteria2.andAnsQueIdEqualTo(question.getQueId());
            List<Answer> list2 = answerMapper.selectByExampleWithBLOBs(answerExample);
            Iterator<Answer> iterator2 = list2.iterator();
            while (iterator2.hasNext()){
                Map<Question,Member> map1 = new HashMap<Question,Member>();
                Map<Answer,Map<Question,Member>> map = new HashMap<Answer,Map<Question,Member>>();
                Answer answer = iterator2.next();
                Member member = memberMapper.selectByPrimaryKey(answer.getAnsUserId());
                map1.put(question,member);
                map.put(answer,map1);
                list.add(map);
                System.out.println("Question");
            }
        }
        AnswerExample answerExample1 = new AnswerExample();
        AnswerExample.Criteria criteria33 = answerExample1.createCriteria();
        criteria33.andAnsUserIdEqualTo(id);
        AncommExample ancommExample = new AncommExample();
        AncommExample.Criteria criteria3 = ancommExample.createCriteria();
        List<Answer> list33 = answerMapper.selectByExampleWithBLOBs(answerExample1);
        Iterator<Answer> iterator33 = list33.iterator();
        while (iterator33.hasNext()){
            Answer answer = iterator33.next();
            criteria3.andAncoTableIdEqualTo(answer.getAnsId());
            List<Ancomm> list3 = ancommMapper.selectByExampleWithBLOBs(ancommExample);
            Iterator<Ancomm> iterator3 = list3.iterator();
            while (iterator3.hasNext()){
                Map<Answer,Member> map1 = new HashMap();
                Map<Ancomm,Map<Answer,Member>> map = new HashMap();
                Ancomm ancomm = iterator3.next();
                Member member = memberMapper.selectByPrimaryKey(ancomm.getAncoUserId());
                map1.put(answer,member);
                map.put(ancomm,map1);
                list.add(map);
                System.out.println("Answer");
            }
        }

        return list;
    }
}
